<template>
  <div class="box">
    <!-- 导航 -->
    <div class="title">
      <div class="title_fan row-wrapper" style="justify-content: space-between">
        <span class="el-icon-arrow-left" @click="$router.go(-1)"></span>
        <span>{{ $t('微信支付') }}</span>
        <span>&nbsp;</span>
      </div>
      <div class="title_back_img">
        <div class="mini_back row-wrapper" style="justify-content: center">
          <!--          <img src="../../../../assets/static/autoRcharge/paynow-logo-blue.svg"-->
          <!--               alt=""/>-->
          <img src="../../../../assets/static/autoRcharge/wechat.svg"
               style="margin-top: 10px" class="wechat"
               alt=""/>
        </div>
        <div class="big_back">
          <div>
            <!--            <div></div>-->
            <el-image :lazy="true"
                      class="qrcode"
                      :preview-src-list="[$route.query.qrcode]"
                      :src="$route.query.qrcode"></el-image>
            <!--            <span class="el-icon-download"></span>-->
          </div>
          <div @click="$router.push({path:'/listafterPayNow',
          query:{order_no:$route.query.order_no}})" class="save">{{ $t('我已支付') }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {paytoken, paycode, recivecode} from '@/api/merbership/pay'
import {getUid} from '@/utils/tokenTools'
import {searchorder} from '@/api/merbership/userpay'
import {balance} from '@/api/merbership/user'
import '@/styles/flax.css'

export default {
  data() {
    return {
      payToken: '',
      qrcode: '',
      flushTime: 30,
      order_id: 1,
      orderInterval: null,
      interval: null,
      balacn: '0.00',
      type: 'pay'
    }
  },
  computed: {
    progress() {
      if (this.flushTime === 30 || this.flushTime === 0) {
        return {
          'width': String(this.flushTime / 30 * 100) + '%',
          'transition': 'width 0s'
        }
      } else {
        return {
          'width': String(this.flushTime / 30 * 100) + '%',
          'transition': 'width 2s'
        }
      }
    }
  },
  activated() {
    // this.paytoken()
    // this.countDown()
    // this.balance()
    // this.flushTime = 30
    // this.searchorderInterval()
  },
  deactivated() {
    clearInterval(this.interval)
  },
  beforeDestroy() {
    clearInterval(this.interval)
  },
  methods: {
    /**
     * 定时轮询订单是否支付成功
     */
    searchorderInterval() {
      this.orderInterval = setInterval(() => {
        this.searchorder()
      }, 3000)
    },
    /**
     * 二维码刷新倒计时
     */
    countDown() {
      let that = this
      this.interval = setInterval(() => {
        that.flushTime -= 1
      }, 1000)
    },
    // -------------------- axios ------------------------
    /**
     * 收款二维码
     */
    recivecode() {
      const pd = {}
      recivecode(this.$qs.stringify(pd))
        .then(res => {
          this.qrcode = res.data.data
        })
    },

    /**
     * 用户余额
     */
    balance() {
      const pd = {}
      balance(this.$qs.stringify(pd))
        .then(res => {
          this.balacn = res.data.data.balance
        })
    },
    /**
     * 查询订单是否支付成功
     */
    searchorder() {
      const pd = {
        order_id: this.order_id
      }
      searchorder(this.$qs.stringify(pd))
        .then(res => {
          this.$message.info(res.data.msg)
        })
    },
    /**
     *
     * 支付二维码
     */
    paycode() {
      const pd = {
        token: this.payToken
      }
      paycode(this.$qs.stringify(pd))
        .then(res => {
          this.qrcode = res.data.data
        })
    },
    /**
     * 支付参数pay token
     */
    paytoken() {
      const pd = {
        order_id: getUid()
      }
      paytoken(this.$qs.stringify(pd))
        .then(res => {
          this.payToken = res.data.data
          this.type = this.$route.query.type
          if (this.$route.query.type === 'pay') {
            this.paycode()
          } else {
            this.recivecode()
          }
        })
    }
  },
  beforeDistory() {
    clearInterval(this.orderInterval)
  },
  watch: {
    flushTime(val) {
      if (val === 0) {
        clearInterval(this.interval)
        this.flushTime = 30
        this.countDown()
        this.paytoken()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.wechat {
  width: 180px;
  height: 40px;
}

.qrcode {
  width: 90%
}

.box {
  padding-bottom: 6%;

  .title {
    background-image: linear-gradient(to bottom, #7AC91B 25%, #f5f5f5 0);

    .title_fan {
      justify-content: space-between;
      color: #fff;
      font-size: 18px;
      padding: 15px 0;
    }
  }

  .title_back_img {
    background-image: url(../../../../assets/static/membership/a5.png);
    background-repeat: no-repeat;
    background-size: 100% 100%;
    padding: 5px 0;

    .mini_back {
      text-align: center;
      font-size: 15px;
      padding: 9% 0;

      > div:nth-child(1) {
        img {
          width: 45px;
        }
      }

      > div:nth-child(3) {
        display: flex;
        justify-content: center;
        align-items: center;
        margin-top: 15px;

        img {
          width: 25px;
          height: 25px;
        }

        span {
          font-size: 28px;
          font-weight: 600;
          margin-left: 13px;
        }
      }
    }

    .big_back {
      margin: 15%;
      text-align: center;
      font-size: 11px;


      > div {
        //height: 236px;
      }

      img {
        width: 90%;
      }

      .progress {
        margin: 5px 0;
        width: 100%;
        height: 10px;
        padding: 5px;
        background-image: url(../../../../assets/static/membership/a2.png);
        background-size: 100% 100%;
        background-repeat: no-repeat;

        .back_progress {
          background-color: #FFB200;
          // width: 30%;
          height: 10px;
          border-radius: 15px;
        }
      }
    }
  }
}

.save {
  //background-color: ;
  background-image: url(../../../../assets/static/autoRcharge/r1.png);
  background-repeat: no-repeat;
  background-size: 100% 100%;
  color: #fff;
  font-size: 14px;
  font-weight: bold;
  padding: 25px 0 35px 0;
  text-align: center;
}
</style>
